Component({
  data: {
    selected: 0,
    color: "#393D49",
    selectedColor: "#e93323",
    list: [{
      pagePath: "/pages/index/index",
      iconPath: "/static/tab-home.png",
      selectedIconPath: "/static/tab-home-current.png",
      text: "首页"
    }, {
      pagePath: "/pages/category/category",
      iconPath: "/static/tab-cate.png",
      selectedIconPath: "/static/tab-cate-current.png",
      text: "分类"
    }, {
      pagePath: "/pages/cart/cart",
      iconPath: "/static/tab-cart.png",
      selectedIconPath: "/static/tab-cart-current.png",
      text: "购物车",
      badge: 0
    }, {
      pagePath: "/pages/user/user",
      iconPath: "/static/tab-my.png",
      selectedIconPath: "/static/tab-my-current.png",
      text: "我的"
    }],
    safeBottom: 0
  },
  
  lifetimes: {
    attached() {
      this.getSafeBottom();
      this.getCartCount();
    }
  },
  
  pageLifetimes: {
    show() {
      // 页面显示时更新选中状态
      this.getSelected();
      this.getCartCount();
    }
  },
  
  methods: {
    // 获取底部安全区域高度
    getSafeBottom() {
      try {
        const systemInfo = wx.getSystemInfoSync();
        // 兼容不同版本小程序的API差异
        if (systemInfo.safeAreaInsets) {
          // 限制安全区域最大高度，避免在小程序下过高
          const safeBottom = Math.min(systemInfo.safeAreaInsets.bottom, 20);
          this.setData({
            safeBottom: safeBottom
          });
        } else if (systemInfo.safeArea) {
          // 旧版本API兼容
          const screenHeight = systemInfo.screenHeight;
          const safeAreaBottom = systemInfo.safeArea.bottom;
          // 限制安全区域最大高度
          const safeBottom = Math.min(screenHeight - safeAreaBottom, 20);
          this.setData({
            safeBottom: safeBottom
          });
        } else {
          // 默认值设置为较小的值
          this.setData({
            safeBottom: 0
          });
        }
      } catch(e) {
        console.log('获取系统信息失败', e);
        this.setData({
          safeBottom: 0
        });
      }
    },
    
    // 获取当前选中的tab
    getSelected() {
      try {
        const pages = getCurrentPages();
        if (!pages || pages.length === 0) return;
        
        const currentPage = pages[pages.length - 1];
        if (!currentPage || !currentPage.route) return;
        
        const route = currentPage.route;
        const tabList = [
          'pages/index/index',
          'pages/category/category',
          'pages/cart/cart',
          'pages/user/user'
        ];
        
        const selected = tabList.findIndex(item => item === route);
        if (selected >= 0 && selected !== this.data.selected) {
          this.setData({
            selected: selected
          });
        }
      } catch(e) {
        console.log('获取当前页面失败', e);
      }
    },
    
    // 获取购物车数量
    getCartCount() {
      try {
        // 从store获取购物车数量
        const app = getApp();
        let cartCount = 0;
        
        if (app && app.globalData && app.globalData.cartCount !== undefined) {
          cartCount = app.globalData.cartCount;
        } else if (app && app.$store && app.$store.state && app.$store.state.app && app.$store.state.app.cartCount !== undefined) {
          // 尝试从 vuex store 获取购物车数量
          cartCount = app.$store.state.app.cartCount;
        }
        
        const list = this.data.list;
        list[2].badge = cartCount > 0 ? cartCount : null;
        this.setData({
          list: list
        });
      } catch(e) {
        console.log('获取购物车数量失败', e);
      }
    },
    
    // 切换tab
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      const selected = data.index;

      // 如果点击的是当前页面，则不处理
      if (this.data.selected === selected) {
        return;
      }
      
      // 先更新状态再切换页面，提升响应速度
      this.setData({
        selected: selected
      });
      
      // 切换页面
      wx.switchTab({
        url: url,
        success: () => {
          
        },
        fail: (err) => {
          // 失败时回滚状态并尝试reLaunch
          this.setData({
            selected: this.data.selected
          });
          wx.reLaunch({
            url: url
          });
        }
      });
    }
  }
});